﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI_CreateRoom : BaseUI {
    public override UIConst UI_Type
    {
        get
        {
            return UIConst.UI_CreateRoom;
        }
    }

    public GameObject mapItem_Prefab;
    public Transform mapItemContent;
    public Button createRoomBtn,leaveBtn;
    public UI_RoomDetail ui_roomDetail;


    private List<UI_MapItem> itemList = new List<UI_MapItem>();
    private MapData curSelectMap;
    private void Awake()
    {
        createRoomBtn.onClick.AddListener(OnCreateRoomBtn);
        leaveBtn.onClick.AddListener(OnLeaveBtn);
    }
    private void OnEnable()
    {
        AllMapData data = MapManager.Instantce.GetMapData();
        SetUIData(data);
    }
    public void SetUIData(AllMapData allMapData)
    {
        for(int i= allMapData.allMap.Count - 1; i< itemList.Count;i++)
        {
            itemList[i].gameObject.SetActive(false);
        }
        for(int i=0; i < allMapData.allMap.Count;i++)
        {
            UI_MapItem item;
            if (i < itemList.Count)
            {
                item = itemList[i];
            }
            else
            {
                item = SpwanItem();
            }
            item.gameObject.SetActive(true);
            var uidata = allMapData.allMap[i];
            item.SetUIData(uidata, () => {
                Log.Info("Selet MapData :" + uidata.ToString());

                curSelectMap = uidata;
                ui_roomDetail.SetUIData(curSelectMap);
            });
        }
    }

   

    private UI_MapItem SpwanItem()
    {
        GameObject go = Instantiate(mapItem_Prefab, mapItemContent);
        var item = go.GetComponent<UI_MapItem>();
        itemList.Add(item);
        return item;
    }

    private void OnLeaveBtn()
    {
        UIManager.Instantce.Close(this.UI_Type,this.ui_Id);
        UIManager.Instantce.Open(UIConst.UI_RoomList);
    }

    private void OnCreateRoomBtn()
    {
        if (curSelectMap == null)
            return;
        RoomMgr.Instantce.RequestCreateRoom(curSelectMap);
    }
}
